package cn.panxing.server.search;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

 

/**
 * This class demonstrates the process of creating an index with Lucene 
 * for text files in a directory.
 */
class TextFileIndexer {
 public static void main(String[] args) throws Exception{
	 
	 TextFileIndexer fileIndex= new TextFileIndexer("/media/sda5/data/book3/","/home/allen/index/");
	 fileIndex.doIndex();
   
 }
   
 public TextFileIndexer(String strSrcDir, String strIndexDir) throws IOException{

	  initIndex(strSrcDir, strIndexDir);
 }

/**
 * @param strSrcDir  
 * @param strIndexDir
 * @throws IOException 
 */
private void initIndex(String strSrcDir, String strIndexDir) throws IOException {
	// fileDir is the directory that contains the text files to be indexed
	   File   fileDir  = new File(strSrcDir);
	   // indexDir is the directory that hosts Lucene's index files
	   File   indexDir = new File(strIndexDir);
	   
	   
	    
	   System.out.println( strSrcDir + strIndexDir );
	   System.out.flush();
	   
	   Analyzer luceneAnalyzer = new StandardAnalyzer();
	   try {
		   indexWriter = new IndexWriter(indexDir,luceneAnalyzer, false);
	   } catch(IOException e){
		   indexWriter = new IndexWriter(indexDir,luceneAnalyzer, true);
	   }
	   textFiles  = fileDir.listFiles();
	   
	   fileSearch = new TextFileSearcher( strIndexDir );   
}

/**
 * @param queryword TODO
 * @param path TODO
 * @return true if thing has been indexed. otherwise return false
 * @throws ParseException
 * @throws IOException
 */
private boolean isIndexed(String queryword, String path ) throws ParseException, IOException {
	
	String results[] = fileSearch.doSearch(queryword);
	
	for(int i=0; i<results.length; ++i){
		if(results[i].equalsIgnoreCase(path))
			return true;
		
		System.out.println(path + ":" + results[i]);
	}
	
	return false;
	
}

public void doIndex() throws IOException, ParseException{
	long startTime = new Date().getTime();
		
	   indexDiretory(textFiles);

	   indexWriter.optimize();
	   indexWriter.close();
	   long endTime = new Date().getTime();
	   
	   System.out.println("It took " + (endTime - startTime) 
	              + " milliseconds to create an index for the files in the directory "
	   				);
}

/**
 * @throws IOException
 * @throws ParseException 
 */
private void indexDiretory(File[] textFiles) throws IOException, ParseException {
	//Add documents to the index
		
	if (null == textFiles)
			return;

	for (int i = 0; i < textFiles.length; i++) {

		if (textFiles[i].isFile()
				&& (textFiles[i].getName().endsWith(".txt")
						|| textFiles[i].getName().endsWith(".pdf") || textFiles[i]
						.getName().endsWith(".chm"))) {
			System.out.println("File " + textFiles[i].getName()
					+ " is being indexed");
			System.out.flush();

			addDocumentToIndex(textFiles[i]);
		} else if (textFiles[i].isDirectory()) {

			System.out.println("File " + textFiles[i].getName()
					+ " is being indexed");
			addDocumentToIndex(textFiles[i]);

			File subDir[] = textFiles[i].listFiles();

			if (subDir != null)
				indexDiretory(subDir);
		}
	}
}

/**
 * @param textFiles
 * @param i
 * @throws IOException
 * @throws ParseException
 */
private void addDocumentToIndex(File textFile) throws IOException, ParseException {
	
	if ( !isIndexed(textFile.getName(), textFile.getPath()) ) {
		Document document = new Document();
	   document.add(new Field("name",textFile.getName(), Store.YES, Index.TOKENIZED));
	   document.add(new Field("path",textFile.getPath(),Store.YES, Index.NO));
	   indexWriter.addDocument(document);
	}
}


     
 
 	private File[] textFiles;
	private IndexWriter indexWriter;
	private TextFileSearcher fileSearch;
}
